package com.chx.controller;

import com.chx.dto.SupplierDto;
import com.chx.dto.SupplierPageQueryDto;
import com.chx.entity.Suppliers;
import com.chx.result.PageResult;
import com.chx.result.Result;
import com.chx.service.SupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author cp-月入过万
 * @Date 2025/5/30
 * 供应商管理
 */
@RestController
@RequestMapping("/admin/supplier")
@Api(tags = "供应商相关接口")
@Slf4j
public class SupplierController {
    @Autowired
    private SupplierService supplierService;

    /**
     * 获取所有供应商列表
     */
    @ApiOperation(value = "获取所有供应商列表")
    @GetMapping("/list")
    @Cacheable(cacheNames = "supplierCache",key = "'list'")
    public Result<List<Suppliers>> getAllSupplier(){
        log.info("获取所有供应商列表");
        List<Suppliers> suppliers = supplierService.getAllSuppliers();
        return Result.success(suppliers);
    }

    /**
     * 新增供应商
     * @param supplierDto
     * @return
     */
    @ApiOperation(value = "新增供应商")
    @PostMapping
    @CacheEvict(cacheNames = "supplierCache",allEntries = true)
    public Result<String> save(@RequestBody SupplierDto supplierDto){
        log.info("新增供应商，供应商数据：{}", supplierDto);
        supplierService.save(supplierDto);
        return Result.success();
    }

    /**
     * 修改供应商
     * @param supplierDto
     * @return
     */
    @ApiOperation(value = "修改供应商信息")
    @PutMapping
    @CacheEvict(cacheNames = "supplierCache",allEntries = true)
    public Result<String> update(@RequestBody SupplierDto supplierDto){
        log.info("修改供应商信息，供应商数据：{}", supplierDto);
        supplierService.update(supplierDto);
        return Result.success();
    }

    /**
     * 删除供应商
     * @param id
     * @return
     */
    @ApiOperation(value = "删除供应商")
    @DeleteMapping("/{id}")
    @CacheEvict(cacheNames = "supplierCache",allEntries = true)
    public Result<String> delete(@PathVariable Long id){
        log.info("删除供应商");
        supplierService.delete(id);
        return Result.success();
    }

    /**
     * 根据id查询供应商信息
     */
    @ApiOperation(value = "根据id查询供应商信息")
    @GetMapping("/{id}")
    @Cacheable(cacheNames = "supplierCache",key = "'id_' + #id")
    public Result<Suppliers> getById(@PathVariable Long id){
        log.info("根据id查询供应商信息");
        Suppliers suppliers = supplierService.getById(id);
        return Result.success(suppliers);
    }

    /**
     * 分页查询供应商信息
     */
    @ApiOperation(value = "分页查询供应商信息")
    @GetMapping("/page")
    public Result<PageResult> page(SupplierPageQueryDto supplierPageQueryDto){
        log.info("分页查询供应商信息");
        PageResult pageResult = supplierService.page(supplierPageQueryDto);
        return Result.success(pageResult);
    }
}
